package com.taotao.sso.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.CookieUtils;
import com.taotao.common.utils.ExceptionUtil;
import com.taotao.sso.pojo.CartItem;
import com.taotao.sso.service.CartItemService;

@Controller
public class CartItemController {
	@Autowired
	private CartItemService cartItemService;

	/**
	 * 发布根据当前token获取当前登录用户购物车信息的服务
	 * 
	 * @param token
	 * @return
	 * @author: JSJ
	 */
	@RequestMapping("/cart/getItem/{token}")
	@ResponseBody
	public TaotaoResult getCartItem(@PathVariable String token) {
		try {
			TaotaoResult result = cartItemService.getCartItemList(token);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}

	/**
	 * 向redis缓存中设置用户购物车信息的服务
	 * 
	 * @param cartItemList
	 * @param request
	 * @return
	 * @author: JSJ
	 */
	@RequestMapping(value = "/cart/setItem/{token}", method = RequestMethod.POST)
	@ResponseBody
	public TaotaoResult setCartItem(@RequestBody List<CartItem> cartItemList,
			HttpServletRequest request,@PathVariable String token) {
		try {
			TaotaoResult result = cartItemService.setCartItemList(cartItemList,
					request, token);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}
}
